package com.csh.szhm.activity;

import android.graphics.Point;
import android.os.Handler;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.LinearLayout;

import com.baidu.mapapi.map.BaiduMap;
import com.baidu.mapapi.map.BaiduMap.OnMapClickListener;
import com.baidu.mapapi.map.BaiduMap.OnMapStatusChangeListener;
import com.baidu.mapapi.map.BaiduMap.OnMarkerClickListener;
import com.baidu.mapapi.map.BitmapDescriptor;
import com.baidu.mapapi.map.BitmapDescriptorFactory;
import com.baidu.mapapi.map.InfoWindow;
import com.baidu.mapapi.map.InfoWindow.OnInfoWindowClickListener;
import com.baidu.mapapi.map.MapPoi;
import com.baidu.mapapi.map.MapStatus;
import com.baidu.mapapi.map.MapStatusUpdate;
import com.baidu.mapapi.map.MapStatusUpdateFactory;
import com.baidu.mapapi.map.MapView;
import com.baidu.mapapi.map.Marker;
import com.baidu.mapapi.map.MarkerOptions;
import com.baidu.mapapi.map.MyLocationData;
import com.baidu.mapapi.map.OverlayOptions;
import com.baidu.mapapi.model.LatLng;
import com.csh.szhm.R;
import com.csh.szhm.utils.AppConfig;

public class SzhmOverlyMainActivity extends MyBaseActivity<String> {
	private int[] titleText = new int[] { R.string.title_txt_1,
			R.string.title_txt_2, R.string.title_txt_3, R.string.title_txt_4,
			R.string.title_txt_5 };
	/**
	 * MapView 是地图主控件
	 */
	private MapView mMapView;

	private BaiduMap mBaiduMap;

	private Marker mMarkerA;

	private Marker mMarkerB;
	private Marker mMarkerC;
	private Marker mMarkerD;
	private InfoWindow mInfoWindow;
	// 初始化全局 bitmap 信息，不用时及时 recycle
	BitmapDescriptor bdGround = BitmapDescriptorFactory
			.fromResource(R.drawable.icon_gcoding);
	private int mActivityType;

	@Override
	public void init() {
		mActivityType = getIntent().getIntExtra("type", 0);
		initContentView(R.layout.szhm_overlay_layout);
		setMyTitle(new Integer[] { selectActivityTitleType(),
				R.drawable.select_list });
		mMapView = (MapView) findViewById(R.id.bmapView);
		initOverlayView();
		mBaiduMap = mMapView.getMap();
		MapStatusUpdate msu = MapStatusUpdateFactory.zoomTo(14.0f);
		mBaiduMap.setMapStatus(msu);
		initOverlay();
		mBaiduMap.setOnMarkerClickListener(new OnMarkerClickListener() {

			public boolean onMarkerClick(final Marker marker) {
				isClick = true;
				final LatLng ll = marker.getPosition();
				MapStatusUpdate u = MapStatusUpdateFactory.newLatLng(ll);
				mBaiduMap.animateMapStatus(u);
				Point p = mBaiduMap.getProjection().toScreenLocation(ll);
				p.y -= 47;
				LatLng llInfo = mBaiduMap.getProjection().fromScreenLocation(p);
				OnInfoWindowClickListener listener = new OnInfoWindowClickListener() {

					@Override
					public void onInfoWindowClick() {
						mBaiduMap.hideInfoWindow();
					}
				};
				mInfoWindow = new InfoWindow(mOverlayView, llInfo, listener);
				mBaiduMap.showInfoWindow(mInfoWindow);
				isClick = false;
				return true;
			}
		});
		mBaiduMap.setOnMapStatusChangeListener(new OnMapStatusChangeListener() {
			@Override
			public void onMapStatusChangeStart(MapStatus arg0) {
				if (!isClick) {
					mBaiduMap.hideInfoWindow();
				}
			}

			@Override
			public void onMapStatusChangeFinish(MapStatus arg0) {
			}

			@Override
			public void onMapStatusChange(MapStatus arg0) {
			}
		});
		mBaiduMap.setOnMapClickListener(new OnMapClickListener() {
			@Override
			public boolean onMapPoiClick(MapPoi paramMapPoi) {
				System.out.println("onMapPoiClick");
				return false;
			}

			@Override
			public void onMapClick(LatLng paramLatLng) {
				if (!isClick) {
					mBaiduMap.hideInfoWindow();
				}
			}
		});
	}

	private int selectActivityTitleType() {
		for (int i = 0; i < titleText.length; i++) {
			if (i == mActivityType) {
				return titleText[i];
			}
		}
		return titleText[0];
	}

	private boolean isClick;
	private View mOverlayView;

	private void initOverlayView() {
		mOverlayView = LayoutInflater.from(this).inflate(
				R.layout.szhm_overlay_item_layout, null);
		LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(
				AppConfig.getScreenWidth() / 5 * 4,
				AppConfig.getScreenWidth() / 4);
		mOverlayView.findViewById(R.id.overlay_root_id).setLayoutParams(lp);
	}

	public void initOverlay() {
		LatLng llB = new LatLng(28.688437, 115.939952);
		LatLng llC = new LatLng(28.68698, 115.9337);
		LatLng llD = new LatLng(28.683874, 115.942899);

		OverlayOptions ooB = new MarkerOptions().position(llB).icon(bdGround)
				.zIndex(5);
		mMarkerB = (Marker) (mBaiduMap.addOverlay(ooB));
		OverlayOptions ooC = new MarkerOptions().position(llC).icon(bdGround)
				.zIndex(5);
		mMarkerC = (Marker) (mBaiduMap.addOverlay(ooC));
		OverlayOptions ooD = new MarkerOptions().position(llD).icon(bdGround)
				.zIndex(5);
		mMarkerD = (Marker) (mBaiduMap.addOverlay(ooD));
		animateMap(llD);
	}

	private void animateMap(final LatLng lad) {
		new Handler().postDelayed(new Runnable() {

			public void run() {
				MapStatusUpdate u = MapStatusUpdateFactory.newLatLng(lad);
				mBaiduMap.animateMapStatus(u);
			}
		}, 1000);
	}

	@Override
	public void click(View v) {
		if (v.getId() == R.id.szhm_title_right_root) {
			finish();
		}
		super.click(v);
	}

	/**
	 * 清除所有Overlay
	 * 
	 * @param view
	 */
	public void clearOverlay(View view) {
		mBaiduMap.clear();
	}

	/**
	 * 重新添加Overlay
	 * 
	 * @param view
	 */
	public void resetOverlay(View view) {
		clearOverlay(null);
		initOverlay();
	}

	@Override
	protected void onPause() {
		// MapView的生命周期与Activity同步，当activity挂起时需调用MapView.onPause()
		mMapView.onPause();
		super.onPause();
	}

	@Override
	protected void onResume() {
		// MapView的生命周期与Activity同步，当activity恢复时需调用MapView.onResume()
		mMapView.onResume();
		super.onResume();
	}

	@Override
	protected void onDestroy() {
		// MapView的生命周期与Activity同步，当activity销毁时需调用MapView.destroy()
		mMapView.onDestroy();
		super.onDestroy();
		// 回收 bitmap 资源
		bdGround.recycle();
	}

}
